home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / launchpadbugs / config.py < prev    next >
Text File  |  2008-08-27  |  3KB  |  79 lines

  1. import ConfigParser
  2. import cStringIO as StringIO
  3. import os.path
  4.  
  5. from lpconstants import CONFIG
  6.  
  7. class Config(dict):
  8.     """ Config system for python-launchpad-bugs
  9.     
  10.     this is working in layers, first the default values (CONFIG.DEFAULT)
  11.     are used, hen the ones defined in the global config file located in
  12.     CONFIG.FILE and at last the configuration set in the file with the
  13.     given filename
  14.     """
  15.     
  16.     MAPPING = dict()
  17.     
  18.     def __init__(self, filename=None, ignore=None):
  19.         d = {}
  20.         self.__filename = filename
  21.         self.__config = ConfigParser.ConfigParser()
  22.         self.__config.readfp(StringIO.StringIO(CONFIG.DEFAULT))
  23.         try:
  24.             self.__config.read(CONFIG.FILE)
  25.         except ConfigParser.MissingSectionHeaderError:
  26.             # if file exists it may be an old style config file which
  27.             # only contains the username
  28.             if os.path.exists(CONFIG.FILE):
  29.                 f = file(CONFIG.FILE, "r")
  30.                 try:
  31.                     self.__config.set("user", "username", f.read().strip(" \n"))
  32.                 finally:
  33.                     f.close()
  34.         if not self.__filename is None:
  35.             self.__config.read(self.__filename)
  36.         ignore = self._create_ignore_dict(ignore)
  37.         for section in self.__config.sections():
  38.             d[section] = {}
  39.             for key, value in self.__config.items(section):
  40.                 if key in ignore[section]:
  41.                     value = ""
  42.                 try:
  43.                     func = self.MAPPING[section][key][0]
  44.                 except (KeyError, IndexError):
  45.                     d[section][key] = value
  46.                 else:
  47.                     d[section][key] = func(value)
  48.         dict.__init__(self, d)
  49.         
  50.     def save(self):
  51.         for section in self.__config.sections():
  52.             for key, value in self.__config.items(section):
  53.                 try:
  54.                     func = self.MAPPING[section][key][1]
  55.                 except (KeyError, IndexError):
  56.                     self.__config.set(section, key, self[section][key])
  57.                 else:
  58.                     self.__config.set(section, key, func(self[section][key]))
  59.         f = file(self.__filename or CONFIG.FILE, "w")
  60.         try:
  61.             f.write(CONFIG.COMMENT)
  62.             self.__config.write(f)
  63.         finally:
  64.             f.close()
  65.  
  66.     def _create_ignore_dict(self, ignore_list):
  67.         ret_dict = dict((s, set()) for s in self.__config.sections())
  68.         if ignore_list is None:
  69.             return ret_dict
  70.         ignore = map(lambda x: x.split("."), ignore_list)
  71.         for i in ignore:
  72.             if i[0] not in ret_dict:
  73.                 continue
  74.             try:
  75.                 ret_dict[i[0]].add(i[1])
  76.             except IndexError:
  77.                 ret_dict[i[0]] = set(self.__config.options(i[0]))
  78.         return ret_dict
  79.